<!DOCTYPE html>
<html lang="zh" xmlns:th="http://www.thymeleaf.org">
<head>
  <title>添加角色</title>
  <div th:replace="common/link::header"></div>
</head>
<body>
<div class="layui-fluid">
  <div class="layui-row">
    <div class="layui-form" lay-filter="sys-role-form" id="sys-role-form">
      <div class="layui-form-item">
        <label class="layui-form-label">角色名称</label>
        <div class="layui-input-inline">
          <input type="text" name="roleName" lay-verify="required" placeholder="请输入角色名称" class="layui-input">
        </div>
      </div>
      <div class="layui-form-item">
        <label class="layui-form-label">角色别名</label>
        <div class="layui-input-inline">
          <input type="text" name="roleKey" lay-verify="required" placeholder="请输入角色别名" class="layui-input">
        </div>
      </div>
      <div class="layui-form-item">
        <label class="layui-form-label">菜单</label>
        <div class="layui-input-inline">
          <div id="menuName" class="xm-select-demo"></div>
        </div>
      </div>
      <div class="layui-form-item">
        <label class="layui-form-label">备注</label>
        <div class="layui-input-inline">
          <textarea class="layui-textarea" name="remark" placeholder="请输入备注"></textarea>
        </div>
      </div>
      <div class="layui-form-item layui-hide">
        <input type="button" lay-submit lay-filter="save-submit" id="save-submit" value="确认">
      </div>
    </div>
  </div>
</div>
<div th:replace="common/script::footer"></div>
<script th:inline="javascript">
  layui.config({
      base: '/static/layuiadmin/' //静态资源所在路径
  }).extend({
     index: 'lib/index', //主入口模块
     xmSelect: 'xm-select'
  }).use(['index', 'form','xmSelect','crud'], function(){
    let $ = layui.$,
            xmSelect = layui.xmSelect,
            crud = layui.crud,
            form = layui.form;
    form.on('submit(save-submit)', function(data){
         let menuIds = crud.getXmSelectTreeIds(select);
         data.field.menuIds = menuIds;
          $.ajax({
              type: 'POST',
              url:  ctx + '/sysRole/add',
              data: JSON.stringify(data.field),
              contentType:'application/json;charset=UTF-8',
              dataType: 'json',
              success: function(result) {
                  layer.msg(result.message);
                  if (result.code === 200) {
                      let index = parent.layer.getFrameIndex(window.name);
                      // 关闭
                      parent.layer.close(index);
                      // 刷新
                      parent.location.reload();
                  }
              }
          });
    });
    let select = xmSelect.render({
        el: '#menuName',
        filterable: true,
        tree: {
          //是否显示树状结构
          show: true,
          //是否展示三角图标
          showFolderIcon: true,
          //是否显示虚线
          showLine: true,
          //间距
          indent: 20,
          //默认展开节点的数组, 为 true 时, 展开所有节点
          expandedKeys: [ -3 ],
          //是否严格遵守父子模式
          strict: false,
          //是否开启极简模式
          simple: false,
          //点击节点是否展开
          clickExpand: true,
          //点击节点是否选中
          clickCheck: true
        },
        toolbar: {
          show: false
        },
        height: 'auto',
        data: [],
        //分页
        paging: true,
        //每页条数
        pageSize: 10,
      });

      $.ajax({
          type: 'get',
          url: ctx + '/sysMenu/xmSelect',
          success: function(res) {
              select.update({
                  data: res
              })
          }
      });
  });
</script>
</body>
</html>